package se.topshelf.golftracker.core.domain;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.text.MessageFormat;

/**
 *
 */
@Entity
@Table(name = "Country", uniqueConstraints = {@UniqueConstraint(columnNames = {"name"})})
public class Country extends BaseEntity implements Comparable<Country>{
    @NotNull
    private String code;

    @NotNull
    private String name;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "continentId")
    private Continent continent;

    public Country() {
    }

    public Country(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Continent getContinent() {
        return continent;
    }

    public void setContinent(Continent continent) {
        this.continent = continent;
    }

    @Override
    public String getGuiDescription() {
        return MessageFormat.format("{0} ({1})", name, code);
    }

    public int compareTo(Country o) {
        return name.compareTo(o.getName());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Country country = (Country) o;

        if (!name.equals(country.name)) return false;

        return true;
    }

    // [8,a,8,0,8,1,c,0,3,0,c,c,3,4,3,4,0,1,3,0,c,c,3,4,6,7,c,1,0,0,0,1]
    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Country");
        sb.append("{code='").append(code).append('\'');
        sb.append(", name='").append(name).append('\'');
        sb.append(", continent=").append(continent);
        sb.append('}');
        return sb.toString();
    }
}
